/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.andremion.louvre.sample.util;

import com.bumptech.glide.util.LogUtil;
import ohos.app.Context;
import ohos.global.resource.NotExistException;
import ohos.global.resource.ResourceManager;
import ohos.global.resource.WrongTypeException;

import java.io.IOException;

/**
 * ResTUtil Class.
 */
public class ResTUtil {
    private static final String TAG = ResTUtil.class.getSimpleName();

    private ResTUtil() {
    }

    /**
     * get the color
     *
     * @param context the context
     * @param id      the id
     * @return the color
     */
    public static int getColor(Context context, int id) {
        int result = 0;
        ResourceManager manager = getResourceManager(context);
        if (manager == null) {
            return result;
        }
        try {
            result = manager.getElement(id).getColor();
        } catch (IOException e) {
            LogUtil.error(TAG, "getColor -> IOException");
        } catch (NotExistException e) {
            LogUtil.error(TAG, "getColor -> NotExistException");
        } catch (WrongTypeException e) {
            LogUtil.error(TAG, "getColor -> WrongTypeException");
        }
        return result;
    }

    /**
     * getString
     *
     * @param context the context
     * @param id      the string id
     * @return string of the given id
     */
    public static String getString(Context context, int id) {
        String result = "";
        ResourceManager manager = getResourceManager(context);
        if (manager == null) {
            return result;
        }
        try {
            result = manager.getElement(id).getString();
        } catch (IOException e) {
            LogUtil.error(TAG, "getString -> IOException");
        } catch (NotExistException e) {
            LogUtil.error(TAG, "getString -> NotExistException");
        } catch (WrongTypeException e) {
            LogUtil.error(TAG, "getString -> WrongTypeException");
        }
        return result;
    }

    /**
     * getInteger
     *
     * @param context the context
     * @param id      the string id
     * @return int of the given id
     */
    public static int getInteger(Context context, int id) {
        int result = 0;
        ResourceManager manager = getResourceManager(context);
        if (manager == null) {
            return result;
        }
        try {
            result = manager.getElement(id).getInteger();
        } catch (IOException e) {
            LogUtil.error(TAG, "getInteger -> IOException");
        } catch (NotExistException e) {
            LogUtil.error(TAG, "getInteger -> NotExistException");
        } catch (WrongTypeException e) {
            LogUtil.error(TAG, "getInteger -> WrongTypeException");
        }
        return result;
    }

    private static ResourceManager getResourceManager(Context context) {
        if (context == null) {
            LogUtil.error(TAG, "getResourceManager -> get null context");
            return null;
        }
        ResourceManager manager = context.getResourceManager();
        if (manager == null) {
            LogUtil.error(TAG, "getResourceManager -> get null ResourceManager");
            return null;
        }
        return manager;
    }
}